import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { defRangeShortcuts } from '@/utils/dateUtil';
import { renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public traceId = ''; // 分销id
  public orderUid = ''; // 订单 uid
  public status = 1; // 状态
  public createdAt = ''; // 创建时间

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  traceId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入分销id',
  },
  orderUid: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入订单 uid',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  // {
  //   field: 'id',
  //   component: 'NInputNumber',
  //   label: 'id',
  //   componentProps: {
  //     placeholder: '请输入id',
  //     onUpdateValue: (e: any) => {
  //       console.log(e);
  //     },
  //   },
  // },
  {
    field: 'traceId',
    component: 'NInput',
    label: '分销 ID',
    componentProps: {
      placeholder: '请输入分销id',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'orderUid',
    component: 'NInput',
    label: '订单 uid',
    componentProps: {
      placeholder: '请输入订单 uid',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NSelect',
    label: '状态',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择状态',
      options: dict.getOption('distributionStatus'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '创建时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'id',
    key: 'id',
    align: 'left',
    width: 50,
  },
  {
    title: '分销id',
    key: 'traceId',
    align: 'left',
    width: 280,
    render(row: State) {
      return h('a', { href: `/#/asset/distribution/distributionDetail/index?traceId=${row.traceId}` }, row.traceId);
    },
  },
  {
    title: '订单 uid',
    key: 'orderUid',
    align: 'left',
    width: -1,
  },
  {
    title: '状态',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('distributionStatus', row.status);
    },
  },
  {
    title: '创建时间',
    key: 'createdAt',
    align: 'left',
    width: -1,
  },
  {
    title: '会员利润分配比例(%)',
    key: 'orderVipProportion',
    align: 'left',
    width: -1,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['distributionStatus']);
}
